package com.lenovo.uitest.power;

import android.R.integer;
import android.app.Instrumentation;
import android.graphics.Rect;
import android.os.Build;
import android.util.Log;
import android.view.KeyEvent;

import com.android.uiautomator.core.Configurator;
import com.android.uiautomator.core.UiCollection;
import com.android.uiautomator.core.UiDevice;
import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.core.UiScrollable;
import com.lenovo.uitest.base.CMP;
import com.lenovo.uitest.base.COM;
import com.lenovo.uitest.base.LAYOUT;
import com.lenovo.uitest.base.LenovoUiTestCase;
import com.lenovo.uitest.base.CTRL;
import com.lenovo.uitest.base.QUICKSET;

public class PowerBase extends LenovoUiTestCase {

	private String statusBarPackage = "com.android.systemui";

	/*
	 * 1. 关机 2. 睡眠模式（进入、退出） 3. 飞行模式（进入、退出） 4. 设置LCD亮度（最亮、最暗） 5.
	 * camera操作（打开、连续对焦、退出） 6. 播放视频（全屏播放、音量设置最大最小） 7. 播放MP3（音量设置最大最小） 8.
	 * 3D游戏（运行Neocore） 9. 上网（打开新浪首页、退出网页，2G/3G/WiFi三种方式） 10. 语音通话（音量最大最小） 11.
	 * 视频通话（音量最大最小）
	 */
	/**
	 * 需求：打开开发人员选项，设定不锁定屏幕
	 */
	public void OpenDevlepNOlockScreen() {

		// callphone("####537999#");
		callPhoneForDesc("####537999#");
  
		UiScrollable scrollable = scrollable(selector().className(
				COM.LISTVIEW));
		UiObject nolock = object(selector().textMatches("不锁定屏幕|Stay\\sawake"));
		UiObject checkObject = getSameLineCheckBox(nolock);

		try {
			if (!checkObject.isChecked()) {
				checkObject.click();
			}
		} catch (UiObjectNotFoundException e) {
			e.printStackTrace();
		}
		pressBack(3);
	}

	public void shutDownForTming() {
		// 使用定时关机
		String time = getCurrentSysTime();
		// 2014-04-06 19:26:57:143
		String hour = time.substring(11, 13);
		String minute = time.substring(14, 16);
		int ap = 0;// AM

		if (Integer.valueOf(hour) >= 12) {
			hour = Integer.valueOf(hour) - 12 + "";
			ap = 1;// PM
		}

		UiScrollable list = scrollable(selector().className(
				"android.widget.ListView"));
		UiObject timing = object(selector().textMatches(
				"定时开关机|Scheduled power on & off"));
		UiObject timing1 = object(selector().textMatches("关机|off"));
		UiObject timing2 = object(selector().textMatches("时间|Time"));
		UiObject repeat = object(selector().textMatches("重复|Repeat"));

		UiObject one = object(selector().className(COM.NUMBERPICKER)
				.instance(0));
		UiObject two = object(selector().className(COM.NUMBERPICKER)
				.instance(1));
		UiObject three = object(selector().className(COM.NUMBERPICKER)
				.instance(2));

		UiObject editHour = object(selector().className(COM.EDITTEXT)
				.instance(0));
		UiObject editMinute = object(selector().className(COM.EDITTEXT)
				.instance(1));
		UiObject editAP = object(selector().className(COM.EDITTEXT)
				.instance(2));

		UiObject done = object(selector().textMatches("完成|Done"));

		try {
			launcherSeting();
			//list.scrollIntoView(timing);
			list.scrollForward(100);
			if(!timing.exists()){
				list.scrollForward(100);
			}
			timing.click();
			timing1.click();
			repeat.click();
			for (int i = 0; i < 7; i++) {
				UiObject checkObject = list.getChild(selector().className(
						COM.CHECKEDTEXTVIES).instance(i));
				if (!checkObject.isChecked()) {
					checkObject.click();
				}
			}
			clickOkAndCancle(CTRL.OK);

			timing2.click();

			// 点击设置时间的三个位置，出现三个editview
			one.click();
			two.click();
			three.click();

			if (editHour.getText().matches("\\d+")) {
				editHour.click();
				inputStrintByKeycode(hour);
				editMinute.click();
				inputStrintByKeycode(Integer.valueOf(minute) + 2 + "");

				String apText = editAP.getText();
				if (ap == 0 && apText.matches("下午|PM")) {
					swipeDirection(editAP, CTRL.DOWN);
				} else if (ap == 1 && apText.matches("上午|AM")) {
					swipeDirection(editAP, CTRL.UP);
				}
			} else {
				// 4.4上上午下午在第一个，小时第二，分钟第三
				editMinute.click();// 0
				inputStrintByKeycode(hour);
				editAP.click();// 1
				inputStrintByKeycode(Integer.valueOf(minute) + 2 + "");

				String apText = editHour.getText();// 3
				if (ap == 0 && apText.matches("下午|PM")) {
					swipeDirection(editHour, CTRL.DOWN);
				} else if (ap == 1 && apText.matches("上午|AM")) {
					swipeDirection(editHour, CTRL.UP);
				}
				device().pressBack();
			}
			done.click();
			done.click();

		} catch (UiObjectNotFoundException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 需求：组件的一个方向，滑向另一个方向，up，down，left，right
	 * 
	 * @param object
	 */
	public void swipeDirection(UiObject object, CTRL CTRL) {
		try {
			Rect rect = object.getBounds();
			int left = rect.left;
			int top = rect.top;
			int right = rect.right;
			int bot = rect.bottom;
			int centx = rect.centerX();
			int centy = rect.centerY();

			if (CTRL == CTRL.UP) {
				device().swipe(centx, bot, centx, top, 10);
			} else if (CTRL == CTRL.DOWN) {
				device().swipe(centx, top, centx, bot, 10);
			} else if (CTRL == CTRL.LEFT) {
				device().swipe(right, centy, left, centy, 10);
			} else if (CTRL == CTRL.RIGHT) {
				device().swipe(left, centy, right, centy, 10);
			}

		} catch (UiObjectNotFoundException e) {
			e.printStackTrace();
		}

	}

	/**
	 * 需求：拨打语音电话,单卡情况
	 * 
	 * @param phoneNum
	 */
	public void callVoicePhone(String phoneNum) {
	
		callphone(phoneNum);
		// 拨号盘在0正下方
		UiObject zeroObject = object(selector().descriptionMatches("零|zero"));
		clickWithBelow(zeroObject, 50);
	}

	public void setWlanAndLinks() {
		setSystemSetting(CTRL.ON, "WLAN");

		UiScrollable scrollable = scrollable(selector().className(
				"android.widget.ListView"));
		UiObject lenovoObject = object(selector().textMatches("lenovo"));
		UiObject scrollObject = object(selector().className(
				"android.widget.ScrollView"));
		UiObject userObject = object(selector().className(
				"android.widget.EditText"));

		try {
			object(selector().textMatches("WLAN")).click();
			scrollable.scrollIntoView(lenovoObject);
		} catch (UiObjectNotFoundException e) {
			e.printStackTrace();
		}

	}

	/**
	 * 需求：拨打视频通话
	 * 
	 * @param phoneNum
	 */
	public void callVideoPhone(String phoneNum) {
		callphone(phoneNum);
	
		device().pressMenu();
		try {
			object(selector().textMatches("视频通话|Video Call")).click();
		} catch (UiObjectNotFoundException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 需求：打开拨号盘，输入号码
	 * 
	 * @param phoneNum
	 */
	public void callphone(String phoneNum) {
		String packageName = "com.lenovo.ideafriend";
		String activity = "com.lenovo.ideafriend.alias.DialtactsActivity";
		LaunchApp(packageName, activity);

		UiObject delObject = object(selector().descriptionMatches(
				"退格|backspace"));
		UiObject textview = object(selector().className(
				"android.widget.TextView"));

		try {
			delObject.longClick();
			textview.setText(phoneNum);
		} catch (UiObjectNotFoundException e) {
			e.printStackTrace();
		}
	}

	public void callPhoneForDesc(String phoneNum) {
		String[] desc = { "one|一", "two|二", "three|三", "four|四", "five|五",
				"six|六", "seven|七", "eight|八", "nine|九", "star|星形符号", "zero|零",
				"pound|英镑符号" };
		String packageName = "com.lenovo.ideafriend";
		String activity = "com.lenovo.ideafriend.alias.DialtactsActivity";
		LaunchApp(packageName, activity);

		UiObject delObject = object(selector().descriptionMatches(
				"退格|backspace"));
		UiObject textview = object(selector().className(
				"android.widget.TextView"));
		char c;
		try {

			delObject.longClick();
			for (int i = 0; i < phoneNum.length(); i++) {
				c = phoneNum.charAt(i);
				switch (c) {
				case '*':
					object(selector().descriptionMatches(desc[9])).click();
					break;
				case '#':
					object(selector().descriptionMatches(desc[11])).click();
					break;
				case '0':
					object(selector().descriptionMatches(desc[10])).click();
					break;

				default:
					object(selector().descriptionMatches(desc[c - 49])).click();
					break;
				}
			}
		} catch (UiObjectNotFoundException e) {
			e.printStackTrace();
		}

	}

	/**
	 * 需求：点击组件下方的组件
	 * 
	 * @param obj
	 * @param ofset
	 */
	public void clickWithBelow(UiObject obj, int ofset) {
		try {
			Rect rect = obj.getBounds();
			int x = rect.centerX();
			int y = rect.bottom + ofset;
			device().click(x, y);
		} catch (UiObjectNotFoundException e) {
			e.printStackTrace();
		}

	}

	/**
	 * 需求：打开浏览器输入，网址
	 * 
	 * @param site
	 */
	public void launchBrowserAndToSite(String site) {

		String packageName = "com.lenovo.browser";
		String activity = "com.lenovo.browser.LeMainActivity";
		LaunchApp(packageName, activity);
		UiObject frameObject = object(selector().className(
				"android.widget.FrameLayout").instance(3));
		UiObject editObject = object(selector().className(
				"android.widget.EditText"));
		UiObject deletObject = object(selector().className("android.view.View")
				.index(1));
		// UiObject
		// goObject=object(selector().className("android.view.View").index(2));
		sleep(800);
		try {
			// 点击输入框
			Rect rect = frameObject.getBounds();
			int centx = rect.centerX();
			int y0 = rect.top + 60;
			device().click(centx, y0);
			// 获取编辑框对象
			deletObject.click();
			editObject.setText(site);
			// goObject.click();
			device().pressEnter();

		} catch (UiObjectNotFoundException e) {
			e.printStackTrace();
		}

	}

	/**
	 * 需求：设置网络模式 AUTO GPRS TD/SCDMA
	 * 
	 * @param CTRL
	 */
	public void setNetData(CTRL CTRL) {
		launcherSeting();
		UiObject object = object(selector().textMatches(
				"Data\\sconnection|数据连接"));
		UiObject moreObject = object(selector().textMatches("更多...|More…"));
		UiScrollable list = scrollable(selector().className(
				"android.widget.ListView"));
		UiObject moibe = object(selector()
				.textMatches("移动网络|Mobile\\snetworks"));
		UiObject netModeObject = object(selector().textMatches(
				"网络模式|Network\\sMode"));
		UiObject sim = object(selector().className(
				"android.widget.RelativeLayout").index(1));
		UiObject ok = object(selector().className("android.widget.Button")
				.textMatches("OK|确定|打开|Open"));
		UiObject LTEOr3G = object(selector().textMatches("启用3G/4G"));

		Rect rect;
		try {
			if (object.exists()) {
				rect = object.getBounds();
				int y0 = rect.top;
				int y1 = rect.bottom;
				UiObject sw = getSameLineSwitch(y0, y1);

				if (!sw.isChecked()) {
					sw.click();// 打开数据链接---k7不一样待修改
				}

			} else {
				moibe.click();
				UiObject data = getSameLineSwitch(object.getBounds().top,
						object.getBounds().bottom);
				if (!data.isChecked()) {
					data.click();
					if (ok.exists()) {
						ok.click();
					}

				}
				UiObject letSwitchObject = getSameLineSwitch(
						LTEOr3G.getBounds().top, LTEOr3G.getBounds().bottom+100);
				if (CTRL == CTRL.TDWCDMA) {
					if (!letSwitchObject.isChecked()) {
						letSwitchObject.click();
					}
				} else if (CTRL == CTRL.GRRS) {
					if (letSwitchObject.isChecked()) {
						letSwitchObject.click();
					}
				}

				return;
			}

			list.scrollIntoView(moreObject);
			moreObject.click();
			moibe.click();
			netModeObject.click();

			sim.click();

			if (CTRL == CTRL.AUTO) {
				object(
						selector().className("android.widget.CheckedTextView")
								.instance(0)).click();
			} else if (CTRL == CTRL.TDWCDMA) {
				object(
						selector().className("android.widget.CheckedTextView")
								.instance(1)).click();
			} else if (CTRL == CTRL.GRRS) {
				object(
						selector().className("android.widget.CheckedTextView")
								.instance(2)).click();
			}

		} catch (UiObjectNotFoundException e) {
			e.printStackTrace();
		}
		// 未完成
	}

	public void pressBack(int num) {
		for (int i = 0; i < num; i++) {
			device().pressBack();
		}
	}

	public void playMp3() {
		String packageName = "com.lenovo.music";
		String activity = "com.lenovo.music.activity.phone.MainActivity";
		LaunchApp(packageName, activity);
		try {
			/*object(selector().className("android.widget.TextView").instance(2))
					.click();*/
			object(selector().textMatches("本地歌曲|Local\\ssongs")).click();
			object(selector().textMatches("全部随机播放|Shuffle\\sall")).click();
		} catch (UiObjectNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 需求：打开视频播放播放第一个视频
	 */
	public void playVideo() {
		String packageName = "com.lenovo.videoplayer";
		String activity = "com.lenovo.videoplayer.VideoPlayerActivity";
		LaunchApp(packageName, activity);
		try {
			object(selector().textMatches("\\d\\d:\\d\\d")).click();
		} catch (UiObjectNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void setVolControl(CTRL CTRL) {
		if (CTRL == CTRL.HIGHT) {
			for (int i = 0; i < 10; i++) {
				device().pressKeyCode(KeyEvent.KEYCODE_VOLUME_UP);
			}
		} else if (CTRL == CTRL.LOW) {
			for (int i = 0; i < 10; i++) {
				device().pressKeyCode(KeyEvent.KEYCODE_VOLUME_DOWN);
			}
		}
	}

	/**
	 * 需求：设置LCD亮度 hight与low
	 * 
	 * @param CTRL
	 */
	public void setLCDLight(CTRL CTRL) {
		launcherSeting();
		UiScrollable scrollable = scrollable(selector().className(
				"android.support.v4.view.ViewPager"));
		UiObject display = object(selector().textMatches("Display|显示"));
		UiScrollable list = scrollable(selector().className(
				"android.widget.ListView"));
		UiObject bright = object(selector().textMatches("Brightness|亮度"));
		UiObject Automatic = object(selector().textMatches(
				"Automatic\\sbrightness|自动调整亮度"));
		UiObject seekBar = object(selector()
				.className("android.widget.SeekBar"));
		try {
			scrollable.scrollIntoView(display);
			display.click();
			list.scrollIntoView(bright);
			bright.click();
			if (Automatic.isChecked()) {
				Automatic.click();
				sleep(500);
			}

			if (CTRL == CTRL.HIGHT) {
				swipeSeekBar(CTRL.HIGHT, seekBar);
			} else if (CTRL == CTRL.LOW) {
				swipeSeekBar(CTRL.LOW, seekBar);
			}
			clickOkAndCancle(CTRL.OK);

		} catch (UiObjectNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void setLCDBrightnessDown(int num) {
		for (int i = 0; i < num; i++) {
			device().pressKeyCode(KeyEvent.KEYCODE_BRIGHTNESS_DOWN);
		}
		clickOkAndCancle(CTRL.OK);
	}

	public void setLCDBrightnessUp(int num) {
		for (int i = 0; i < num; i++) {
			device().pressKeyCode(KeyEvent.KEYCODE_BRIGHTNESS_UP);
		}
		clickOkAndCancle(CTRL.OK);
	}

	/**
	 * 需求：滑动滑块，到最高或者最低
	 * 
	 * @param CTRL
	 * @param seekBar
	 */
	public void swipeSeekBar(CTRL CTRL, UiObject seekBar) {
		try {
			Rect rect = seekBar.getBounds();
			int x0 = rect.left;
			int x1 = rect.right;
			int centy = rect.centerY();

			if (CTRL == CTRL.HIGHT) {
				device().swipe(x0 + 5, centy, x1, centy, 20);
			} else if (CTRL == CTRL.LOW) {
				device().swipe(x1 - 20, centy, x0, centy, 30);
			}

		} catch (UiObjectNotFoundException e) {
			e.printStackTrace();
		}

	}

	/**
	 * 需求：打开状态栏
	 */
	public void showStatusBar() {
		int width = device().getDisplayWidth();
		int hight = device().getDisplayHeight();
		do {
			device().swipe(width / 2, 5, width / 2, hight, 10);
		} while (!device().getCurrentPackageName().equals(statusBarPackage));
	}

	/**
	 * 需求：隐藏状态栏
	 */
	public void hideStatusBar() {
		device().pressBack();
	}

	/**
	 * 需求：请调用QUICKSET 常量,进行设置
	 * 
	 * @param quickSeting
	 */
	public void clickQuickSeting(String quickSeting) {
		showStatusBar();
		try {
			object(selector().className("android.widget.ImageView").index(2))
					.click();
			// wlan对象，无文字为第一个对象
			UiObject gridViewObj = object(
					selector().className("android.widget.GridView")).getChild(
					selector().index(0));
			if (quickSeting == "wlan") {
				gridViewObj.click();
			} else {
				object(selector().textMatches(quickSeting)).click();
			}
		} catch (UiObjectNotFoundException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 需求：点击对话框架，CTRL.OK/CTRL.CANCEL
	 * 
	 * @param i
	 */
	public void clickOkAndCancle(CTRL CTRL) {
		UiObject ok = object(selector().className("android.widget.Button")
				.textMatches("OK|确定|打开|Open"));
		UiObject cancle = object(selector().className("android.widget.Button")
				.textMatches("Cancel|取消|关闭|Close"));
		try {
			if (CTRL == CTRL.CANCEL) {
				cancle.click();
			} else if (CTRL == CTRL.OK) {
				ok.click();
			}
		} catch (UiObjectNotFoundException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 需求：用于设置飞行模式的开关
	 * 
	 * @param CTRL
	 */
	public void setAirplaneForSeting(CTRL CTRL) {
		setSystemSetting(CTRL, "Airplane\\smode|飞行模式");
		UiObject ok = object(selector().className("android.widget.Button")
				.textMatches("OK|确定"));
		if (ok.exists()) {
			try {
				ok.click();
			} catch (UiObjectNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

	public void setWlanCTRL(CTRL CTRL) {
		setSystemSetting(CTRL, "WLAN");
	}

	/**
	 * 需求：设置系统设置开关,或者设置其他如系统设置一样的开关
	 * 
	 * @param CTRL
	 * @param regel
	 */
	public void setSystemSetting(CTRL CTRL, String regel) {
		launcherSeting();
		UiObject text;
		text = object(selector().textMatches(regel));
		if (regel.matches("WLAN")) {//如果是wlan则使用文本完全匹配
			text = object(selector().text(regel));
		}
		

		Rect rect;
		try {
			rect = text.getBounds();
			int y0 = rect.top;
			int y1 = rect.bottom;
			UiObject CTRLObject = getSameLineSwitch(y0, y1);
			if (CTRLObject==null) {
				text.click();//如果等于空，则无对应的switch，点击进入，进行设置
				CTRLObject=getSameLineSwitch(text.getBounds().top, text.getBounds().bottom);
			}
			
			if (CTRL == CTRL.ON) {
				if (!CTRLObject.isChecked()) {
					CTRLObject.click();
				}
			} else if (CTRL == CTRL.OFF) {
				if (CTRLObject.isChecked()) {
					CTRLObject.click();
				}
			}
		} catch (UiObjectNotFoundException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 需求：获取同一行的开关控件
	 * 
	 * @param y0
	 * @param y1
	 * @return
	 */
	public UiObject getSameLineSwitch(int y0, int y1) {
		UiCollection collection = collection(selector().index(0));
		UiObject CTRLObject = null;
		try {
			int num=collection.getChildCount(selector().className(COM.SWITCH));
			for (int i = 0; i < num; i++) {
				CTRLObject = collection.getChildByInstance(selector()
						.className("android.widget.Switch"), i);

				Rect rect = CTRLObject.getBounds();
				int centy = rect.centerY();
				if (centy > y0 && centy < y1) {
					return CTRLObject;
				}
			}
		} catch (UiObjectNotFoundException e) {			
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 需求：获得同一行的checkbox
	 * 
	 * @param object
	 * @return
	 */
	public UiObject getSameLineCheckBox(UiObject object) {
		Rect r1;
		UiCollection collection = collection(selector().index(0));
		UiObject CheckObject = null;
		try {
			r1 = object.getBounds();
			int y0 = r1.top;
			int y1 = r1.bottom;
			for (int i = 0; i < 10; i++) {
				CheckObject = collection.getChildByInstance(selector()
						.className(COM.CHECKBOX), i);
				Rect rect = CheckObject.getBounds();
				int centy = rect.centerY();
				if (centy > y0 && centy < y1) {
					return CheckObject;
				}
			}

		} catch (UiObjectNotFoundException e) {
			e.printStackTrace();
		}
		return CheckObject;
	}

	/**
	 * 需求：通过菜单键启动到系统设置
	 */
	public void launcherSeting() {
		device().pressHome();
		device().pressHome();
		device().pressMenu();
		try {
			object(selector().textMatches("Settings|系统设置")).click();
			object(selector().textMatches("System|系统设置")).click();
			swipeDownScreen(2);
		} catch (UiObjectNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
    
	/**
	 * 需求：通过exec来启动设置
	 */
	public void launcherSettingForExec() {
		String packageName = "com.android.settings";
		String activity = "com.android.settings.Settings";
		LaunchApp(packageName, activity);
		try {
			object(selector().textMatches("System|系统设置")).click();
			swipeDownScreen(3);
		} catch (UiObjectNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 需求：用于向上滚动
	 * 
	 * @param num
	 */
	public void swipeDownScreen(int num) {
		int width = device().getDisplayWidth();
		int hight = device().getDisplayHeight();
		for (int i = 0; i < num; i++) {
			device().swipe(width / 2, hight / 4, width / 2, hight, 10);
		}
	}

	/**
	 * 需求：用于向下滚动
	 * 
	 * @param num
	 */
	public void swipeUpScreen(int num) {
		int width = device().getDisplayWidth();
		int hight = device().getDisplayHeight();
		for (int i = 0; i < num; i++) {
			device().swipe(width / 2, 3 * hight / 4, width / 2, hight / 4, 10);
		}
	}

	public void startVideo() {
		// 快门
		UiCollection start_video = collection(selector().textMatches(
				"android.widget.FrameLayout"));
		UiObject start1;
		try {
			start1 = start_video.getChildByInstance(selector()
					.descriptionMatches(".*快门.*|Shutter\\sbutton"), 0);

			UiObject start2 = start_video.getChildByInstance(selector()
					.descriptionMatches(".*快门.*|Shutter\\sbutton"), 1);
			UiObject video = object(selector().descriptionMatches(
					".*快门.*|Shutter\\sbutton"));
			if (start2.exists()) {
				Rect rect2 = start2.getBounds();
				device().click(rect2.centerX(), rect2.centerY());
			} else if (start1.exists()) {
				Rect rect1 = start1.getBounds();
				device().click(rect1.centerX(), rect1.centerY());
			} else if (video.exists()) {
				video.click();
			} else {
				int X = device().getDisplayWidth();
				int Y = device().getDisplayHeight();
				device().click(X / 2 + 80, Y - 80);
			}
		} catch (UiObjectNotFoundException e) {
			e.printStackTrace();
		}
	}

	public void closVideo() {
		startVideo();
	}

	// 未实现
	public void camaraSettingAFC() {
		// 设置连续对焦模式
		device().pressMenu();
		System.out.println(isCamaraVirtical());
		UiScrollable listObject = scrollable(selector().className(
				COM.EXPANDBLELISTVIES));
		System.out.println(isCamaraVirtical());
		UiObject afcObject = object(selector().textMatches("对焦方式"));
		System.out.println(isCamaraVirtical());
		try {
			listObject.scrollIntoView(afcObject);
			swipeDirection(listObject, CTRL.UP);
		} catch (UiObjectNotFoundException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 需求：判断相机是否为竖屏//不能正确判断
	 * 
	 * @return
	 */
	public boolean isCamaraVirtical() {
		UiScrollable listObject = scrollable(selector().className(
				COM.EXPANDBLELISTVIES));
		try {
			UiObject oneRelative = listObject.getChild(selector().className(
					LAYOUT.RELATIVE).index(2));
			Rect rect = oneRelative.getBounds();
			int x0 = rect.left;
			System.out.println(x0 + " " + rect.right);
			if (x0 < 50) {
				return true;
			}
		} catch (UiObjectNotFoundException e) {
			e.printStackTrace();
		}
		return false;
	}

	public void Startphotograph() {
		clickShutter(1);
	}

	/**
	 * 需求：按快门，第几个快门，用于多个快门的相机
	 */
	public void clickShutter(int i) {
		UiCollection start_video = collection(selector().textMatches(
				"android.widget.FrameLayout"));
		UiObject start1;
		try {
			start1 = start_video.getChildByInstance(selector()
					.descriptionMatches(".*快门.*|Shutter\\sbutton"), 0);

			UiObject start2 = start_video.getChildByInstance(selector()
					.descriptionMatches(".*快门.*|Shutter\\sbutton"), 1);
			UiObject video = object(selector().descriptionMatches(
					".*快门.*|Shutter\\sbutton"));
			if (i == 2) {
				Rect rect2 = start2.getBounds();
				device().click(rect2.centerX(), rect2.centerY());
			} else if (i == 1) {
				Rect rect1 = start1.getBounds();
				device().click(rect1.centerX(), rect1.centerY());
			}
		} catch (UiObjectNotFoundException e) {
			e.printStackTrace();
		}
	}

	// 获取系统版本号
	public String getSYSVersion() {
		String sdk = Build.VERSION.RELEASE;
		return sdk;
	}

	/**
	 * 需求：快速连续单机屏幕
	 * 
	 * @param num
	 * @param x
	 * @param y
	 */
	public void quickClick(int num, int x, int y) {
		if (getSYSVersion().matches(".*4\\.4.*")) {
			long dis = Configurator.getInstance()
					.getActionAcknowledgmentTimeout();
			Configurator.getInstance().setActionAcknowledgmentTimeout(10);
			for (int i = 0; i < num; i++) {
				device().click(x, y);
			}
			Configurator.getInstance().setActionAcknowledgmentTimeout(dis);
		}
	}

	/**
	 * 需求：判断视频播放是否是全屏
	 * 
	 * @return
	 */
	public boolean isFullSreenVideo() {
		UiObject view = object(selector().className(COM.VIEW));
		Rect rect;
		try {
			rect = view.getBounds();
			int y0 = rect.top;
			if (y0 == 0) {
				return true;
			}
		} catch (UiObjectNotFoundException e) {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 需求：设置视频播放为全屏播放
	 */
	public void setVideoFullScreen() {
		int width = device().getDisplayWidth();
		int height = device().getDisplayHeight();
		int i = 0;
		while (!isFullSreenVideo() && i++ < 5) {
			quickClick(3, width / 2, height / 2);
		}
	}
	/**
	 * 需求：设置快捷开关,部分按钮可靠
	 * @param QUICKSET
	 */
	public void setQuickSetting(String QUICKSET,CTRL CTRL){
		
		UiObject button=object(selector().className(COM.BUTTON));
		UiObject quick=object(selector().textMatches(QUICKSET));				
		try {
			showStatusBar();
			button.click();
			boolean b=isQuickOn(quick);
			
			if (CTRL==CTRL.ON) {
				if (b==false) {
					button.click();
				}
			}else if(CTRL==CTRL.OFF){
				if (b==true) {
					button.click();
				}
			}
		} catch (UiObjectNotFoundException e) {
			e.printStackTrace();
		}
		
	}
	/**
	 * 需求：判断快速设置开关是否是开，部分按钮可靠
	 * @param object
	 * @return
	 */
	public boolean isQuickOn(UiObject object) {
		try {
			int centx=object.getBounds().centerX();
			int y0=object.getBounds().top;
			int y1=object.getBounds().bottom;
			
			for (int i = y0; i < y1; i=i+10) {
				int color=getColorFofPixel(centx, i);
				if (color>-10000000) {
					return true;					
				}
				System.out.println(color);
			}
		} catch (UiObjectNotFoundException e) {
			e.printStackTrace();
		}
		return false;
	}
	/**
	 * 需求：设置屏幕待机时间，second,minute,15s，30s,1m,2m,5m,10m,30m
	 * @param time
	 */
	public void setDelayed(CTRL CTRL) {
		launcherSeting();
		UiScrollable list=scrollable(selector().className(COM.LISTVIEW));
		UiObject display = object(selector().textMatches("Display|显示"));
		UiObject sleep=object(selector().textMatches("屏幕待机|Sleep"));
//		String[] delayStr={"15 秒|15 seconds","30 秒|30 seconds",
//				"1 分钟|1 minute","2 分钟|2 minute","5 分钟|5 minute",
//				"10 分钟|10 minute","30 分钟|30 minute"};
		
		String[] delayStr={"(15.秒)|(15.seconds)","(30.秒)|(30.seconds)",
				"(1.分钟)|(1.minute)","(2.分钟)|(2.minute)","(5.分钟)|(5.minute)",
				"(10.分钟)|(10.minute)","30.分钟"};
		try {
			list.scrollIntoView(display);
			display.click();
			sleep.click();
			
			switch (CTRL) {
			case SECOND_15:
				object(selector().textMatches(delayStr[0])).click();
				break;
			case SECOND_30:
				object(selector().textMatches(delayStr[1])).click();
				break;
			case MINUTE_1:
				object(selector().textMatches(delayStr[2])).click();
				break;
			case MINUTE_2:
				object(selector().textMatches(delayStr[3])).click();
				break;
			case MINUTE_5:
				object(selector().textMatches(delayStr[4])).click();
				break;
			case MINUTE_10:
				object(selector().textMatches(delayStr[5])).click();
				break;
			case MINUTE_30:
				object(selector().textMatches(delayStr[6])).click();
				break;
			default:
				break;
			}
			
		} catch (UiObjectNotFoundException e) {
			e.printStackTrace();
		}
		
	}

}
